<template>
  <div class="login">
    <van-nav-bar title="个人注册页" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <van-field
        v-model="repassword"
        type="password"
        name="确认密码"
        label="确认密码"
        placeholder="确认密码"
        :rules="[{ required: true, message: '请填写确认密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
    <router-link :to="{ name: 'Login' }">已有账号去登录</router-link>
  </div>
</template>

<script>
import { Notify } from "vant";
import { regAPI } from "../services/reg";
import { setToken } from "../utils/auth";
export default {
  data() {
    return {
      username: "",
      password: "",
      repassword: "",
    };
  },
  methods: {
    async onSubmit() {
      if (this.password != this.repassword) {
        Notify({
          message: "两次密码输入不一致",
          type: "error",
        });
        return
      } else {
        const data = { userName: this.username, password: this.password };
        const result = await regAPI(data);
        if (result.code == 1) {
          Notify({
            message: "注册成功",
            type: "success",
          });
          setToken(result.data);
          this.$router.replace({
             name:"User"
          })
        } else {
          Notify({
            message: "注册失败",
            type: "error",
          });
        }
      }
    },
  },
};
</script>

<style></style>
